﻿--[[
@brief  游戏Ping值帮助类
@author jameszhai
@date   2017-08-17
--]]

--[[
BROADCAST_BEGIN(Msg_UpdatePing)
    JSONB(int, game);               // Game ID
    JSONB(map<int,int>, ping);      // PING Map
BROADCAST_END

SERVICE_BEGIN(Svr_GetGamePing)
    JSONB(int, game);               // 游戏ID
    JSONG(vector<int>, zone);       // 大区ID列表
SERVICE_END

SERVICE_BEGIN(Svr_StopPing)
    JSONB(int, game);               // 停止PING
SERVICE_END
--]]

TPingHelper = 
{
    Init = function()
        local pTPFUI = base_utility.get_component_adapter("ITpf_ui",
                            "ierd_tgp.tpf_ui.GetTPFUI();");

        if nil ~= pTPFUI then
            pTPFUI:AddLuaListener(ierd_tgp.accelerate_module.ACCE_ON_ROUTE_REFRESH, 
                                  "TPingHelper.OnRouteRefresh");
        end;

        comm_center.reg_service("Svr_GetGamePing", "TPingHelper.OnSvrGetPing");
        comm_center.reg_service("Svr_StopPing", "TPingHelper.OnSvrGetPing");
    end;

    UpdatePing = function()
        if false == TPingHelper.bUpdated then
            TGPLog.LogI("PingHelper", "Update ping, route not refreshed.");
            return;
        end;

        if TPingHelper.nGameID   <= 0 or nil == TPingHelper.zZoneIDs or
           #TPingHelper.zZoneIDs <= 0 then
            TGPLog.LogI("PingHelper", "Update ping, parameter error.");
            return;
        end;

        local pAccModule = base_utility.get_component_adapter("IAccelerate_module",
                "ierd_tgp.accelerate_module.GetAcceModule();");

        if nil == pAccModule then
            TGPLog.LogE("PingHelper", "Get net acc module fail.");
            return;
        end;

        if nil == banner_model then
            TGPLog.LogE("PingHelper", "Banner model is null.");
            return;
        end;

        local   zSvrs = banner_model.get_game_servers(TPingHelper.nGameID);
        local   nMode = 0;

        if nil ~= zSvrs then
            nMode = zSvrs.net_mode;
        else
            TGPLog.LogE("PingHelper", "Get game server fail.");
        end;
        
        local   zInfo = ierd_tgp.accelerate_module.IDCSpeedInfoPOD:new();
        local   zTemp = {};

        for nIdx = 1, #TPingHelper.zZoneIDs do
            local   nZoneId = TPingHelper.zZoneIDs[nIdx];
            local   nDelay  = pAccModule:get_real_ping(TPingHelper.nGameID, nZoneId);

            if (-1 == nDelay) then
                if true == pAccModule:get_zone_speed_info(TPingHelper.nGameID, nZoneId, nMode,zInfo) then
                    -- 使用了加速
                    if 0 == zInfo.decision or 4 == zInfo.decision or
                       8 == zInfo.decision then
                        nDelay = zInfo.proxy_delay_raw_t1 + zInfo.proxy_delay_raw_t2;
                    else
                        nDelay = zInfo.direct_delay_raw;
                    end;
                else
                    TGPLog.LogE("PingHelper", "Get zone speed information fail.");
                end;
            end;

            if -1 ~= nDelay then
                local   zItem = {};

                zItem["zone_id"] = nZoneId;
                zItem["ping"   ] = nDelay;

                table.insert(zTemp, zItem);
            end;
        end;

        zInfo:delete();

        local zData = {};

        zData["game"] = TPingHelper.nGameID;
        zData["ping"] = zTemp;

        comm_center.send_broadcast("Msg_UpdatePing", zData);
    end;

    OnSvrGetPing = function(zParam)
        TPingHelper.nGameID  = zParam["game"];
        TPingHelper.zZoneIDs = zParam["zone"];

        TPingHelper.UpdatePing();
    end;

    OnSvrStopUpdate = function(zParam)
        TPingHelper.nGameID = 0;
    end;

    OnRouteRefresh = function()
        TPingHelper.bUpdated = true;
        TPingHelper.UpdatePing();
    end;

    bUpdated = false;       -- PING值是否已经更新
    nGameID  = 0;           -- 游戏ID
    zZoneIDs = {};          -- 大区ID列表

};